#include "stdafx.h"
#include "Triangle.h"
#include "ShaderTypes.h"
#include "Renderer.h"

using namespace DirectX;
using namespace Rendering;

Triangle::Triangle(Renderer* renderer, string vertexShaderFile, string pixelShaderFile)
{
	// Load the vertex array with data.
	m_vertices.push_back(PosColorVertex(XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)));  // Bottom left.
	m_vertices.push_back(PosColorVertex(XMFLOAT3(0.0f, 1.0f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)));  // Bottom left.
	m_vertices.push_back(PosColorVertex(XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f)));  // Bottom left.

	// Load the index array with data.
	unsigned short indices[3];
	indices[0] = 0;  // Bottom left.
	indices[1] = 1;  // Top middle.
	indices[2] = 2;  // Bottom right.

	this->SetShader(new Shader(renderer, vertexShaderFile, pixelShaderFile, PosColorVertexLayout, ARRAYSIZE(PosColorVertexLayout)));
	this->SetMesh(new GenericMesh<PosColorVertex>(renderer->Device(), static_cast<UINT>(m_vertices.size()), m_vertices.data(), 6, indices));
}


Triangle::~Triangle(void)
{
}

/// <summary>Transforms the vertices.</summary>
/// <param name="renderer">The renderer to render this object.</param>
/// <param name="matrix">The transformation matrix.</param>
void Triangle::Transform(_In_ Renderer* renderer, _In_ DirectX::CXMMATRIX matrix)
{
	std::vector<PosColorVertex> vertices;
	for(auto vertex : m_vertices)
	{
		PosColorVertex transformedVertex = vertex;
		XMStoreFloat3(&transformedVertex.Pos, XMVector3Transform(XMLoadFloat3(&vertex.Pos), matrix));
		vertices.push_back(transformedVertex);
	}
	this->GetMesh()->SetVertices(renderer->Device(), static_cast<UINT>(vertices.size()), vertices.data());
}